package com.eyevision.common.widget

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import com.cade.agriculturemall.R
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import rx.subscriptions.CompositeSubscription
import java.util.concurrent.TimeUnit

/**
 * Created by tom on 17/7/31.
 */
class ProgressAnimation(context: Context?, attrs: AttributeSet?) : View(context, attrs) {

    private val mPaint: Paint = Paint()
    private var mCount = 5
    private var mAnimationSet: AnimatorSet? = null

    private val mRadius = px_f(10)
    private val mSpace = px_i(2)


    fun View.px_i(dp: Int): Int {
        if (dp in intArrayOf(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) {
            return dp
        }
        return dp * context.resources.displayMetrics.density.toInt()
    }

    fun View.px_f(dp: Int): Float {
        if (dp in intArrayOf(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) {
            return dp.toFloat()
        }
        return dp * context.resources.displayMetrics.density
    }
    private val mArrayFloat: MutableList<Float> = mutableListOf()

    init {
        mPaint.color = context!!.resources.getColor(R.color.colorDarkPink)
        mPaint.isAntiAlias = true
    }

    private val mStartX by lazy {
        (width - mRadius * 2 * mCount - (mCount - 1) * mSpace) / 2
    }

    private val mY by lazy {
        height.toFloat() / 2
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        mAnimationSet?.let {
            if (it.isRunning) {
                var x = mStartX + mRadius
                for (i in 0..mCount - 1) {
                    canvas?.drawCircle(x, mY, mArrayFloat[i], mPaint)
                    x += mSpace + mRadius * 2
                }
            }
        }


    }

    fun start() {
        if (mAnimationSet == null) {
            mAnimationSet = AnimatorSet()
            mAnimationSet?.let {
                mArrayFloat.clear()
                val mArray: MutableList<ValueAnimator> = mutableListOf()
                for (i in 0..mCount - 1) {
                    mArrayFloat.add(0.0f)
                    val v = ValueAnimator.ofFloat(0.0f, mRadius)
                    v.duration = 600
                    v.repeatCount = ValueAnimator.INFINITE
                    v.repeatMode = ValueAnimator.REVERSE
                    v.startDelay = 100 * i.toLong()
                    v.addUpdateListener {
                        mArrayFloat[i] = it.animatedValue as Float
                        postInvalidate()
                    }
                    mArray.add(v)
                }

                it.duration = mCount * 120.toLong()
                it.playTogether(mArray as Collection<Animator>?)
            }

        }
        mAnimationSet?.let {
            if (!it.isRunning) {
                it.start()
            }
        }


    }

    fun clear() {
        mAnimationSet?.let {
            if (it.isRunning) {
                it.cancel()
            }
        }
    }


    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        start()
    }


    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        clear()
    }

}